#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

bool Widget::QtSaveFontImage()
{
    charSize = ui->comboBox->currentText().toInt();
    QFont xFont(ui->fontComboBox->currentFont());

    ui->plainTextEdit->clear();
    xFont.setPixelSize(charSize/4);
    ui->plainTextEdit->setFont(xFont);
    ui->plainTextEdit->appendPlainText(text);
    qDebug() << text;




    //    QFont xFont("Open Sans", 50);
    //    QFont xFont;

    int imgSize = charSize + 10;

    QImage image(imgSize, imgSize, QImage::Format_Mono);
    QPainter painter(&image);
    painter.setFont(xFont);

    QString foldName = "../" + xFont.family();
    QDir d;
    d.mkdir(xFont.family());

    for(int i = 0; i < text.size(); ++i) {
        painter.fillRect(image.rect(), Qt::black);
        painter.setPen(QColor(Qt::white));
        painter.drawText(image.rect(), Qt::AlignCenter | Qt::AlignVCenter, text.at(i));
        image.save(foldName + "/" + text.at(i) + tr(".png"));
    }

    QMessageBox::information(this, "提示","汉字图片已成功生成",QMessageBox::Ok);
    return true;
}



bool Widget::GetText()
{
    text = ui->plainTextEdit->toPlainText();
    //    qDebug() << text;
    if (text.isEmpty()){
        //        QMessageBox::warning(this, "警告","请输入文本",QMessageBox::Ok);
        textFileName = QFileDialog::getOpenFileName(this, "选择文本", "../../");
        QFile textFile(textFileName);
        textFile.open(QIODevice::ReadOnly);
        QTextStream s(&textFile);
        text = s.readLine();
        textFile.close();
        ui->plainTextEdit->appendPlainText(text);
        //        return 0;
    }

    int originalLength = text.size();
    wstring s = text.toStdWString()/*L"汉字"*/;
    //        qDebug() << QString::fromStdWString(s) + "  问";
    //        qDebug() << (uint)s.at(0);
    sort(s.begin(), s.end());
    //        qDebug() << QString::fromStdWString(s) + "  问";

    wstring::iterator newEnd = unique(s.begin(), s.end());
    s.erase(newEnd, s.end());

    //        text.clear();
    text = QString::fromStdWString(s);
    //        qDebug() << text;
    QMessageBox::information(this,
                             "录入文本",
                             tr("已经获取文本\n") +
                             "共 " + QString::number(originalLength) + " 个汉字\n" +
                             "去除重复后，剩余 "+ QString::number(text.size()) + " 个汉字",
                             QMessageBox::Ok);
    return true;

}
